/*
 * @Author: Joel
 * @Date: 2025-04-09 10:17:12
 * @LastEditors: Joel
 * @LastEditTime: 2025-04-09 14:54:13
 * @FilePath: /gd32f303cct6/usr/board_driver/button_input.h
 * @Description: 
 * Copyright (C) 2025 YiJiaLink, All Rights Reserved. 
 * 2025-04-09 10:17:12
 */

 #ifndef __BUTTON_INPUT_H
 #define __BUTTON_INPUT_H
 /* 包含头文件代码 */
 
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>

#include "gd32f30x.h"

 #if defined(__cplusplus)
 extern "C" {
 #endif /*_cplusplus*/

/* 电源键 PA0 */
#define WAKEUP_KEY_PIN                   GPIO_PIN_0
#define WAKEUP_KEY_GPIO_PORT             GPIOA
#define WAKEUP_KEY_GPIO_CLK              RCU_GPIOA
#define WAKEUP_KEY_EXTI_LINE             EXTI_0
#define WAKEUP_KEY_EXTI_PORT_SOURCE      GPIO_PORT_SOURCE_GPIOA
#define WAKEUP_KEY_EXTI_PIN_SOURCE       GPIO_PIN_SOURCE_0
#define WAKEUP_KEY_EXTI_IRQn             EXTI0_IRQn  

/* 上键 PC13 */
#define UP_KEY_PIN                   GPIO_PIN_13
#define UP_KEY_GPIO_PORT             GPIOC
#define UP_KEY_GPIO_CLK              RCU_GPIOC
#define UP_KEY_EXTI_LINE             EXTI_13
#define UP_KEY_EXTI_PORT_SOURCE      GPIO_PORT_SOURCE_GPIOC
#define UP_KEY_EXTI_PIN_SOURCE       GPIO_PIN_SOURCE_13
#define UP_KEY_EXTI_IRQn             EXTI10_15_IRQn  
/* 下键 PB02 */
#define DOWN_KEY_PIN                   GPIO_PIN_2
#define DOWN_KEY_GPIO_PORT             GPIOB
#define DOWN_KEY_GPIO_CLK              RCU_GPIOB
#define DOWN_KEY_EXTI_LINE             EXTI_2
#define DOWN_KEY_EXTI_PORT_SOURCE      GPIO_PORT_SOURCE_GPIOB
#define DOWN_KEY_EXTI_PIN_SOURCE       GPIO_PIN_SOURCE_2
#define DOWN_KEY_EXTI_IRQn             EXTI2_IRQn  
/* 确认键 PB05 */
#define CONFIRM_KEY_PIN                   GPIO_PIN_5
#define CONFIRM_KEY_GPIO_PORT             GPIOB
#define CONFIRM_KEY_GPIO_CLK              RCU_GPIOB
#define CONFIRM_KEY_EXTI_LINE             EXTI_5
#define CONFIRM_KEY_EXTI_PORT_SOURCE      GPIO_PORT_SOURCE_GPIOB
#define CONFIRM_KEY_EXTI_PIN_SOURCE       GPIO_PIN_SOURCE_5
#define CONFIRM_KEY_EXTI_IRQn             EXTI5_9_IRQn  
/* 充电状态输入 PB13 */
#define CHARGING_PIN                   GPIO_PIN_13
#define CHARGING_GPIO_PORT             GPIOB
#define CHARGING_GPIO_CLK              RCU_GPIOB
#define CHARGING_EXTI_LINE             EXTI_13
#define CHARGING_EXTI_PORT_SOURCE      GPIO_PORT_SOURCE_GPIOB
#define CHARGING_EXTI_PIN_SOURCE       GPIO_PIN_SOURCE_13
#define CHARGING_EXTI_IRQn             EXTI10_15_IRQn  


/* 按键触发标志 */
struct usr_key
{
    uint8_t power_key : 1;
    uint8_t up_key : 1;
    uint8_t down_key : 1;
    uint8_t confirm_key :1;
    uint8_t charging_input :1;
    uint8_t not_use :3; 
};
/* 按键滤波标志 */
struct button_filter
{
    uint16_t power_index;
    uint16_t up_index;
    uint16_t down_index;
    uint16_t confirm_index;
    uint16_t charg_index;
};
#define KEY_FILTER_TIME 100
#define KEY_LONG_PRESSED_TIME 2000
#define KEY_CLICK_TIME 1000
/* 按键连击标志 */
struct button_click
{
    /* 连击次数 */
    struct
    {
        uint16_t power_click : 3;
        uint16_t up_click : 3;
        uint16_t down_click : 3;
        uint16_t confirm_click : 3;
        uint16_t charg_click : 4;
    };
    /* 连击间隔时间 */
    uint16_t power_click_timer;
    uint16_t up_click_timer;
    uint16_t down_click_timer;
    uint16_t confirm_click_timer;
    uint16_t charg_click_timer;
};


void button_all_init(void);
struct usr_key *get_button_pressed(void);

#if defined(__cplusplus)
}
#endif /*_cplusplus*/

#endif
/* el psy congroo */
